package cn.dnuihyy406.dnuifood_16110100406.controller.collectfragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.bean.CollectBean;
import cn.dnuihyy406.dnuifood_16110100406.bean.ResultBean;
import cn.dnuihyy406.dnuifood_16110100406.controller.getfood.GetFoodActivity;
import cn.dnuihyy406.dnuifood_16110100406.listener.OnRetrofitListener;
import cn.dnuihyy406.dnuifood_16110100406.model.BaseModel;
import cn.dnuihyy406.dnuifood_16110100406.model.GetfoodModel;

public class CFoodAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnRetrofitListener<ResultBean> {
    private Context mContext;
    private List<CollectBean> CollectList1;
    private LayoutInflater mLayoutInflater;
    SharedPreferences sharedPreferences;
    String user_id,foodid;
    public CFoodAdapter(Context mContext,List CollectList1){
        this.mContext=mContext;
        this.CollectList1=CollectList1;
        mLayoutInflater=LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v =mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.c_food_list_item,viewGroup,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder Holder,final int i) {
        sharedPreferences = mContext.getSharedPreferences("hyy", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getString("userid","");
        final CollectBean x=CollectList1.get(i);
        if (null==x){
            return;
        }
        ViewHolder viewHolder=(ViewHolder) Holder;
        viewHolder.c_price.setText(x.getPrice()+"元");
        viewHolder.c_foodname.setText(x.getFoodname());
        Picasso.with(mContext).load(BaseModel.BaseUrl+x.getPic()).into(viewHolder.c_foodpic);
        viewHolder.enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str = String.valueOf(CollectList1.get(i).getFood_id());
                Intent intent=new Intent(mContext, GetFoodActivity.class);
                intent.putExtra("food_id",str);
                mContext.startActivity(intent);
                Toast.makeText(mContext,x.getFoodname(),Toast.LENGTH_LONG).show();
            }
        });

        viewHolder.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                uncollect(x.getFood_id());
            }
        });
    }

    @Override
    public int getItemCount() {
        return CollectList1.size();
    }

    @Override
    public void onSuccess(ResultBean object, int flag) {
        if(flag==6){
            if(object.getSuccess().equals("1")){
                Toast.makeText(mContext,"取消了",Toast.LENGTH_SHORT).show();
                for (int i=0;i<CollectList1.size();i++){
                    CollectBean y=CollectList1.get(i);
                    if(y.getFood_id().equals(foodid)){
                        CollectList1.remove(y);
                        notifyDataSetChanged();
                        break;
                    }
                }
            }
        }
    }

    @Override
    public void onFailure(String msg) {

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView c_foodname,c_price;
        ImageView c_foodpic;
        Button enter,cancel;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            c_foodname = itemView.findViewById(R.id.c_food_name);
            c_foodpic = itemView.findViewById(R.id.c_food_pic);
            c_price = itemView.findViewById(R.id.c_price);
            enter=itemView.findViewById(R.id.c_enter);
            cancel=itemView.findViewById(R.id.c_quxiao);
        }
    }

    private void uncollect(String food_id){
        this.foodid=food_id;
        GetfoodModel getfoodModel=new GetfoodModel();
        getfoodModel.userCollectFood(user_id,food_id,this);
    }
}
